package vn.hocweb.dao.dao_implement;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import vn.hocweb.dao.dao_interface.ITrangDAO;
import vn.hocweb.entity.Trang;

public class TrangDAO extends GenericDAO implements ITrangDAO{

	@Override
	public Trang findByID(Long id) {
		Connection connection = getConnection();
		Trang result = null;
		String sql = "select * from trang where macongcu = ?";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setLong(1, id);
			ResultSet effected = pre.executeQuery();
			if (effected.next()){
				long maTrang = effected.getLong("matrang");
				
				String ten = effected.getString("ten");
				String moTa = effected.getString("mota");
				int thuTu = effected.getInt("thutu");
				boolean noiBat = effected.getBoolean("noibat");
				boolean hienThi = effected.getBoolean("hienthi");
				
				long maDanhMuc = effected.getLong("madanhmuc");
				
				result = new Trang(thuTu, noiBat, hienThi, 
						maTrang, maDanhMuc, ten, moTa);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public List<Trang> findAll(int firstItem, int batchSize) {
		Connection connection = getConnection();
		List<Trang> result = new ArrayList<Trang>();
		String sql = "select * from trang limit ?, ?";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setLong(1, firstItem);
			pre.setLong(2, batchSize);
			ResultSet effected = pre.executeQuery();
			while (effected.next()){
				long maTrang = effected.getLong("matrang");
				
				String ten = effected.getString("ten");
				String moTa = effected.getString("mota");
				int thuTu = effected.getInt("thutu");
				boolean noiBat = effected.getBoolean("noibat");
				boolean hienThi = effected.getBoolean("hienthi");
				
				long maDanhMuc = effected.getLong("madanhmuc");
				
				Trang row = new Trang(thuTu, noiBat, hienThi, 
						maTrang, maDanhMuc, ten, moTa);
				result.add(row);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public int size() {
		Connection connection = getConnection();
		int result = 0;
		String sql = "select count(*) from trang";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			ResultSet effected = pre.executeQuery();
			if(effected.next()){
				result = effected.getInt(0);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public boolean save(Trang entity) {
		Connection connection = getConnection();
		boolean result = false;
		String sql = "insert into trang (ten, mota, thutu, noibat, hienthi, madanhmuc) " +
				"values (?, ?, ?, ?, ?, ?)";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setString(1, entity.getTen());
			pre.setString(2, entity.getMoTa());
			pre.setInt(3, entity.getThuTu());
			pre.setBoolean(4, entity.isNoiBat());
			pre.setBoolean(5, entity.isHienThi());
			pre.setLong(6, entity.getDanhMuc().getMaDanhMuc());
			
			int effected = pre.executeUpdate();
			if(effected > 0) result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public boolean delete(Trang entity) {
		Connection connection = getConnection();
		boolean result = false;
		String sql = "delete from trang where matrang = ?";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setLong(1, entity.getMaTrang());
			
			int effected = pre.executeUpdate();
			if(effected > 0) result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public boolean remove(Trang entity) {
		Connection connection = getConnection();
		boolean result = false;
		String sql = "update trang set hienthi = ? where matrang = ?";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setBoolean(1, false);
			pre.setLong(2, entity.getMaTrang());
			
			int effected = pre.executeUpdate();
			if(effected > 0) result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public boolean restore(Trang entity) {
		Connection connection = getConnection();
		boolean result = false;
		String sql = "update trang set hienthi = ? where matrang = ?";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setBoolean(1, true);
			pre.setLong(2, entity.getMaTrang());
			
			int effected = pre.executeUpdate();
			if(effected > 0) result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public List<Trang> findAll_danhSachTrang(long maDM, int firstItem,
			int batchSize) {
		Connection connection = getConnection();
		List<Trang> result = new ArrayList<Trang>();
		String sql = "select * from trang where madanhmuc = ? limit ?, ?";
		PreparedStatement pre;
		try {
			pre = connection.prepareStatement(sql);
			pre.setLong(1, maDM);
			pre.setInt(2, firstItem);
			pre.setInt(3, batchSize);
			ResultSet effected = pre.executeQuery();
			while (effected.next()){
				long maTrang = effected.getLong("matrang");
				
				String ten = effected.getString("ten");
				String moTa = effected.getString("mota");
				int thuTu = effected.getInt("thutu");
				boolean noiBat = effected.getBoolean("noibat");
				boolean hienThi = effected.getBoolean("hienthi");
				
				long maDanhMuc = effected.getLong("madanhmuc");
				
				Trang row = new Trang(thuTu, noiBat, hienThi, 
						maTrang, maDanhMuc, ten, moTa);
				result.add(row);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

}
